package com.ww.word.ui;

import java.io.InputStream;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.ww.core.activity.MyActivity;
import com.ww.core.util.CoreSharedPreferencesHelper;
import com.ww.core.util.FileUtil;
import com.ww.core.widget.scroll.MyScrollLayout;
import com.ww.word.R;

/**
 * .
 * @version 2013-3-22
 * @author wangw
 */
public class GuideActivity extends MyActivity {

	private Button startBtn;
	private TextView tip;
	private ProgressBar progressbar;
	private MyScrollLayout scrollLayout;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_guide);
		initView();
		runTask();
	}

	/**
	 * 后台任务.
	 */
	public void runTask() {
		new Thread(new Runnable() {
			@Override
			public void run() {
				InputStream is = getResources().openRawResource(R.raw.daydayup);
				FileUtil.copyDbFile(is, Constants.DATABASE_PATH, Constants.DATABASE_NAME);
				CoreSharedPreferencesHelper h = new CoreSharedPreferencesHelper(GuideActivity.this);
				String str = h.getValue("num");
				if (str == null) {
					h.setValue("num", "5");
				}
				handler.sendEmptyMessage(1);
			}
		}).start();
	}

	// 定义一个Handler，用来异步处理数据
	private Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case 1:
				startBtn.setVisibility(View.VISIBLE);
				progressbar.setVisibility(View.INVISIBLE);
				tip.setText(R.string.guide_inited);
				tip.setVisibility(View.INVISIBLE);
				break;
			}
		};
	};

	/**
	 * 初始化控件.
	 */
	private void initView() {
		scrollLayout = (MyScrollLayout) findViewById(R.id.ScrollLayout);
		scrollLayout.setmOrientation(MyScrollLayout.VERTICAL);
		progressbar = (ProgressBar) findViewById(R.id.xlistview_footer_progressbar);
		tip = (TextView) findViewById(R.id.tip);
		startBtn = (Button) findViewById(R.id.btn);
		startBtn.setOnClickListener(onClick);
	}

	private View.OnClickListener onClick = new View.OnClickListener() {
		@Override
		public void onClick(View v) {
			Intent intent = new Intent(GuideActivity.this, StudyActivity.class);
			GuideActivity.this.startActivity(intent);
			GuideActivity.this.finish();
		}
	};

}